package com.rock.code.bigdata.kbp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.rock.code.base.service.impl.BaseServiceImpl;
import com.rock.code.bigdata.kbp.dao.TbSysKbpDao;
import com.rock.code.bigdata.kbp.domain.TbSysKbp;
import com.rock.code.bigdata.kbp.service.TbSysKbpService;
import com.rock.code.common.handover.DataSource;
import com.rock.code.common.pager.Page;
import com.rock.code.common.tree.AdditionalParameters;
import com.rock.code.common.tree.ItemNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author piandu
 * @description Service实现
 * @date 2018/9/14 16/34
 */
@Service
public class TbSysKbpServiceImpl extends BaseServiceImpl<TbSysKbpDao> implements TbSysKbpService {

    @Resource
    private TbSysKbpDao tbSysKbpDao;

    /**
     * Insert.
     *
     * @param
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */


    @Override
    public int insert(TbSysKbp tbSysKbp) throws Exception {
        return 0;
    }

    /**
     * Delete.
     *
     * @param primaryKey the primary key
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int delete(String primaryKey) throws Exception {
        return tbSysKbpDao.delete(primaryKey);
    }

    /**
     * Delete by param.
     *
     * @param param the param
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int deleteByParam(Map<String, Object> param) throws Exception {
        return tbSysKbpDao.deleteByParam(param);
    }

    @Override
    public int update(TbSysKbp tbSysKbp) throws Exception {
        return 0;
    }


    @Override
    public TbSysKbp query(TbSysKbp tbSysKbp) throws Exception {
        return tbSysKbpDao.query(tbSysKbp);
    }

    /**
     * Query by para map.
     *
     * @param param the param
     * @return the tbSysModule
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public TbSysKbp queryByParaMap(Map<String, Object> param) throws Exception {
        return tbSysKbpDao.queryByParaMap(param);
    }

    /**
     * Query list.
     *
     * @param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public List<TbSysKbp> queryList(TbSysKbp tbSysKbp) throws Exception {
        return tbSysKbpDao.queryList(tbSysKbp);
    }

    /**
     * Query list by para map.
     *
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public List<TbSysKbp> queryListByParaMap(Map<String, Object> param) throws Exception {
        return tbSysKbpDao.queryListByParaMap(param);
    }

    /**
     * Query page list.
     *
     * @param param the param
     * @return the page info
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public PageInfo queryPageList(Page page, Map<String, Object> param) throws Exception {
        String orderBy = "";
        if (!StringUtil.isEmpty(page.getSidx())) {
            orderBy = this.getOrderBy(page);
        }
        PageHelper.startPage(page.getPage(), page.getRows());
        if (!orderBy.equals("")) {
            PageHelper.orderBy(orderBy);
        }
        List<TbSysKbp> list = tbSysKbpDao.queryListByParaMap(param);
        return new PageInfo(list);
    }

     /**
     * Query list by order.
     *
     * @param page  the page
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    public List<TbSysKbp> queryListByOrder(Page page, Map<String, Object> param) throws Exception {
        String orderBy = "";
        if (!StringUtil.isEmpty(page.getSidx())) {
            orderBy = this.getOrderBy(page);
        }
        if (!orderBy.equals("")) {
            param.put("orderBy", orderBy);
        }
        return tbSysKbpDao.queryListByParaMap(param);
    }

    /**
     * Query tree data.
     *
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    public List<ItemNode> queryTreeData(Map<String, Object> param) throws Exception {
        List<TbSysKbp> tbSysKbps = tbSysKbpDao.queryListByParaMap(param);
        List<ItemNode> itemNodes = new ArrayList<>();
        for (TbSysKbp tbSysKbp : tbSysKbps){
            ItemNode itemNode = new ItemNode();
            itemNode.setText(tbSysKbp.getKbpName());
            itemNode.setType(tbSysKbp.getIsLeaf() == 1 ? "item" : "folder");
            AdditionalParameters additionalParameters = new AdditionalParameters();
            additionalParameters.setId(tbSysKbp.getKbpCode());
            additionalParameters.setTreeLevel(tbSysKbp.getTreeLevel());
            additionalParameters.setModulePath(tbSysKbp.getKbpPath());
            additionalParameters.setDataIcon("ace-icon fa fa-bars");
            itemNode.setAdditionalParameters(additionalParameters);
            itemNodes.add(itemNode);
        }
        return itemNodes;
    }

    @Override
    public List<TbSysKbp> queryUserRoleModuleByParaMap(Map<String, Object> param) throws Exception {
        return tbSysKbpDao.queryUserRoleModuleByParaMap(param);
    }






    @Override
    @DataSource("bigdata")
    public List queryPage(Map map) {
        return tbSysKbpDao.queryPage(map);
    }



}